var app = angular.module('myApp', []);

app.factory('dataService', ['$http', function($http) {
  return {
    queryList: function() {
      return $http({
        method: "GET",
        url: "js/data.json"
      });
    }
  };

}]);

app.factory('audio', ['$document', function($document){
  var audio = $document[0].createElement("audio");
  return {
    node: audio,
    setUrl: function(url){
      this.node.src = url;
      this.play();
    },
    play: function(){
      this.node.play();
    },
    pause: function(){
      this.node.pause();
    },
    stop: function(){
      this.node.stop();
    }
  };
}]);

app.factory('player', ['audio', function(audio) {

  return {
    playstatus: false,
    pausestatus:false,
    play: function(url) {
      if(url){
        audio.setUrl(url);
      }else {
        audio.play();
      }
      this.pausestatus = false;
      this.playstatus = true;
    },
    stop : function(){
      audio.stop();
       this.pausestatus = false;
      this.playstatus = false;
    },
    pause: function(){
      audio.pause();
      this.pausestatus = true;
      this.playstatus = false;
    }
  };

}]);

app.directive('player', function(){
  return {
    'restrict': 'E',
    'replace': true,
    templateUrl: "tpl/player.html",
    link : function(scope, element, attrs){

    }
  };
});

app.controller('firstController', ['$scope', '$http', 'dataService', 'player', function($scope, $http, dataService, player) {
  $scope.player = player;
  dataService.queryList().success(function(data, status) {
    $scope.infos = data;
  });


}]);